package com.smedi.ismedi.inmail.core.port.adapter.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.weather.WeatherHourly;
import com.smedi.ismedi.inmail.core.domain.repository.WeatherHourlyRepository;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.model.WeatherHourlyEntity;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.mybatis.mapper.WeatherHourlyMapper;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;

/**
 * 基础信息仓库实现类
 *
 * @author jiangqinxuan
 * @date 2021/08/03
 */
@Repository
@AllArgsConstructor
public class WeatherHourlyRepositoryImpl extends ServiceImpl<WeatherHourlyMapper, WeatherHourlyEntity> implements WeatherHourlyRepository, IService<WeatherHourlyEntity> {
    private final DozerUtil dozerUtil;

    private final WeatherHourlyMapper weatherHourlyMapper;


    @Override
    public void insertWeatherHourly(@NonNull WeatherHourly weatherHourly) {
        WeatherHourlyEntity weatherHourlyEntity = dozerUtil.convertor(weatherHourly, WeatherHourlyEntity.class);
        weatherHourlyMapper.insert(weatherHourlyEntity);
    }

    @Override
    public String getWeatherByLocationIdAndTime(String name, LocalDateTime time) {
        LambdaQueryWrapper<WeatherHourlyEntity> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(WeatherHourlyEntity::getName, name);
        queryWrapper.eq(WeatherHourlyEntity::getTime, time);
        queryWrapper.last("limit 1");

        WeatherHourlyEntity weatherHourlyEntity = this.baseMapper.selectOne(queryWrapper);
        if (null != weatherHourlyEntity) {
            return weatherHourlyEntity.getText();
        }

        return "";
    }
}